package com.witmed.home.view

import android.app.ProgressDialog
import android.graphics.Rect
import android.os.Bundle
import android.util.DisplayMetrics
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.PagerSnapHelper
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.shuyu.gsyvideoplayer.GSYVideoManager
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer
import com.shuyu.gsyvideoplayer.video.base.GSYVideoView.CURRENT_STATE_NORMAL
import com.witmed.home.R
import com.witmed.home.databinding.FragmentHomeBinding
import com.witmed.home.view.adapter.VideoAdapter
import com.witmed.home.view.intent.VideoIntent
import com.witmed.home.view.viewmodel.VideoState
import com.witmed.home.view.viewmodel.VideoViewModel
import com.witmed.lib_common.ActivityPath
import com.witmed.lib_http.entity.VideoList
import com.witmed.lib_http.entity.VideoListItem
import kotlinx.coroutines.launch


@Route(path = ActivityPath.PAGE_HOME)
class HomeFragment : Fragment() {
    lateinit var db:FragmentHomeBinding
    lateinit var vm:VideoViewModel
    lateinit var dialog:ProgressDialog
    lateinit var videoAdapter:VideoAdapter
    lateinit var rv: RecyclerView
    var list= VideoList()
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        db= FragmentHomeBinding.inflate(inflater)
        vm=ViewModelProvider(this).get(VideoViewModel::class.java)

        rv=db.rv

        videoAdapter= VideoAdapter(requireContext(),list,object :VideoAdapter.ItemClickListener{
            override fun onClick(position: Int, entity: VideoListItem) {
                ARouter.getInstance().build(ActivityPath.PAGE_DETAIL).withSerializable("video",entity).navigation()
            }

        },object :VideoAdapter.LongClickListener{
            override fun onLongClick(entity: VideoListItem) {
                ARouter.getInstance().build(ActivityPath.PAGE_DETAIL).withSerializable("video",entity).navigation()
            }

        })

       /* val dm = DisplayMetrics()
        activity?.windowManager?.defaultDisplay?.getMetrics(dm)
        val helper=ScrollCalculatorHelper(R.id.vp,
            dm.heightPixels/2-DpTools.dip2px(requireContext(),180),
            dm.heightPixels/2+DpTools.dip2px(requireContext(),180))

        //列表有ViewPager的翻页效果
        val pagerSnapHelper = PagerSnapHelper()
        pagerSnapHelper.attachToRecyclerView(rv)*/

        val manager=LinearLayoutManager(requireContext())
        rv.apply {
            adapter=videoAdapter
            layoutManager=manager
        }
        
        /*rv.addOnScrollListener(object :RecyclerView.OnScrollListener(){

            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                super.onScrollStateChanged(recyclerView, newState)
                helper.onScrollStateChanged(recyclerView,newState)
            }

            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)

                val firstVisibleItem = manager.findFirstVisibleItemPosition();
                val lastVisibleItem = manager.findLastVisibleItemPosition();
                helper.onScroll(recyclerView,firstVisibleItem,lastVisibleItem,1)

            }
        })*/

        lifecycleScope.launch {
            vm.videoState.collect{
                when(it){
                    is VideoState.Loading->{
                        dialog=ProgressDialog(context)
                        dialog.setTitle("加载中……")
                        dialog.create()
                        dialog.show()
                    }
                    is VideoState.Sucess->{
                        dialog.dismiss()
                        list.addAll(it.list)

                        videoAdapter.notifyDataSetChanged()
                    }
                    is VideoState.Error->{
                        dialog.dismiss()
                    }

                    else -> {}
                }
            }
        }


        lifecycleScope.launch {
            vm.videoChannel.send(VideoIntent.videoList(1,10))
        }
        return db.root
    }

    /*override fun onPause() {
        super.onPause()
        GSYVideoManager.onPause()
    }
    override fun onResume() {
        super.onResume()
        GSYVideoManager.onResume()
    }
    override fun onDestroy() {
        super.onDestroy()
        GSYVideoManager.releaseAllVideos()
    }*/
}